package com.example.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.ResponseResult;
import com.example.demo.entity.Supplier;
import com.example.demo.mapper.SupplierMapper;
import com.example.demo.service.SupplierService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {
    @Override
    public ResponseResult getList() {
        List<Supplier> suppliers = list();

        if(list().isEmpty()){
            return ResponseResult.ok("没有供应商信息");
        }

        return  ResponseResult.ok(suppliers);
    }

    @Override
    public ResponseResult getByName(String name) {
        if(StrUtil.isBlank(name)){
            return ResponseResult.fail("请输入正确的供应商名称");
        }
        List<Supplier> suppliers = list(new QueryWrapper<Supplier>().eq("Supplier_name", name));

        if(suppliers.isEmpty()){
            return ResponseResult.ok("没有对应的供应商信息");
        }

        return ResponseResult.ok(suppliers);
    }

    @Override
    public ResponseResult saveSupplier(Supplier supplier) {
        if(supplier == null){
            return ResponseResult.fail("请输入正确的供应商信息");
        }
        boolean isSuccess = save(supplier);
        if(!isSuccess){
            return ResponseResult.fail("新增失败");
        }
        return ResponseResult.ok("新增成功");
    }

    @Override
    public ResponseResult updateSupplier(Supplier supplier) {
        if(supplier == null){
            return ResponseResult.fail("请输入正确的供应商信息");
        }
        boolean isSuccess = update(supplier, new QueryWrapper<Supplier>().eq("Supllier_name", supplier.getName()));

        if(!isSuccess){
            return ResponseResult.fail("更新失败");
        }
        return ResponseResult.ok("更新成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(String id) {
        boolean isSuccess = remove(new QueryWrapper<Supplier>().eq("Supplier_id", id));

        if(!isSuccess){
            return ResponseResult.fail("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    /**
     * 根据名称删除
     * @param name
     * @return
     */
    @Override
    public ResponseResult deleteByName(String name) {
        boolean isSuccess = remove(new QueryWrapper<Supplier>().eq("Supplier_name", name));

        if(!isSuccess){
            return ResponseResult.fail("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }
}
